package com.atguigu.lease.web.admin.interceptors;

import com.atguigu.lease.common.result.ResultCodeEnum;
import com.atguigu.lease.common.springmvc.LeaseException;
import com.atguigu.lease.common.utils.JwtUtils;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.util.ObjectUtils;
import org.springframework.web.servlet.HandlerInterceptor;

/**
 * @author:高尚
 * @data:2025/7/12 15:45
 * @description:
 */
public class LoginInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //1.获取token 请求头（access_token)
        String accessToken = request.getHeader("access_token");
        if(ObjectUtils.isEmpty(accessToken)){
            throw new LeaseException(ResultCodeEnum.ADMIN_LOGIN_AUTH);// 没令牌 还没登陆呢
        }
        //2.有token，检查是否过期
        JwtUtils.parseToken(accessToken);
        return true;
    }
}
